package com.yidin.logic;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yidin.entry.AccountDetailEntry;
import com.yidin.entry.MyDataEntry;
import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.MyFriendListEntry;
import com.yidin.entry.MyPhotoDetailEntry;
import com.yidin.entry.NearByStoreEntry;
import com.yidin.entry.NearbyFriendsEntry;
import com.yidin.entry.NotificationCenterEntry;
import com.yidin.entry.PhotoListEntry;
import com.yidin.entry.ServiceBack;
import com.yidin.entry.StoreDetailEntry;
import com.yidin.entry.UserLoadCommentListEntry;
import com.yidin.entry.UserLoadGiftListEntry;
import com.yidin.lsc.BaseActivity;
import com.yidin.lsc.LscActivity;
import com.yidin.lsc.lscManager;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class MainService extends Service implements Runnable {

	public static boolean isRun = false;
	public static MainService mainService = null;
	private httpCon myHttpCon;
	private String ver = "&pv=1.0&appv=1.0";
	public static String SERVECE_IP = "http://124.237.121.75:8000/webservice.aspx?";
	protected JSONObject pager = new JSONObject();
	// public static UserInfo nowUser = null;
	/**
	 * 所有任务。
	 */
	private static ArrayList<Task> allTask = new ArrayList<Task>();
	/**
	 * 所有ui对象。
	 */
	private static ArrayList<BaseActivity> allActivity = new ArrayList<BaseActivity>();

	public MainService() {
		mainService = this;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		Log.d("server", "创建server");
		isRun = true;
		mainService = this;
		try {
			pager.put("PageIndex", "1");
			pager.put("ReturnCount", "20");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.myHttpCon = httpCon.getDefault(this);
		Thread th = new Thread(this);
		th.start();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
	}

	@Override
	public void run() {
		Log.d("server", "start run!");
		while (isRun) {
			// Log.d("server", "main server run!");
			Task lasttask = null;
			synchronized (allTask) {
				if (allTask.size() > 0) {
					lasttask = allTask.get(0);
					DoTask(lasttask);
				}
			}
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 执行任务。
	 * 
	 * @param lasttask
	 */
	private String recvBuffer = "";

	private void DoTask(Task lasttask) {
		Message msg = hand.obtainMessage();
		Log.d("server", "task id=" + lasttask.getTaskID());

		try {
			switch (lasttask.getTaskID()) {
			case Task.TASK_USER_LOGIN: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("Account", params.get("AccountId"));
				obj.put("Password", params.get("Password"));
				obj.put("DeviceInfo", params.get("DeviceInfo"));
				obj.put("AccountType", params.get("AccountType"));
				// obj.put("source", "android");

				// URLEncoder.encode("http://124.237.121.75:8000/webservice.aspx?m=uim&a=UserLogIn&p={ "AccountId":"hlf", "Password":"123456", "Deviceinfo":"iPhone4S"}&pv=1.0&appv=1.0","UTF-8");
				String url = SERVECE_IP + "m=uim&a=UserLogIn&p="
						+ URLEncoder.encode(obj.toString(), "UTF-8")
						+ "&pv=1.0&appv=1.0";
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_REGISTER: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Password", params.get("Password"));
				obj.put("Email", params.get("Email"));
				obj.put("Sex", params.get("Sex"));
				obj.put("Membername", params.get("Membername"));
				obj.put("Telephone", params.get("Telephone"));
				String url = SERVECE_IP + "m=uim&a=UserRegister&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getRegisterData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_RESET_PASSWORD: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("Email", params.get("Email"));
				String url = SERVECE_IP + "a=UserResetPassword&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getRegisterData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_HOME: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONArray objInterfaceName = new JSONArray();
				objInterfaceName.put("UserLoadPersonalPage");
				objInterfaceName.put("UserLoadGiftList");

				JSONArray objInterfaceCcotent = new JSONArray();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("AccountFrom", params.get("AccountId"));
				obj.put("Pager", pager);
				objInterfaceCcotent.put(obj.toString());
				objInterfaceCcotent.put(obj.toString());

				String url = SERVECE_IP
						+ "Composite=1&a="
						+ myHttpCon.getEncordUrl(objInterfaceName.toString())
						+ "&p="
						+ myHttpCon
								.getEncordUrl(objInterfaceCcotent.toString())
						+ ver;
				Bundle b = new Bundle();
				MyDataEntry me = new MyDataEntry();
				List<UserLoadGiftListEntry> l = new ArrayList<UserLoadGiftListEntry>();
				msg.obj = JsonUtil.getUserLoadPersonalPage(
						myHttpCon.doGet(url), l, me);
				b.putString("e", me.e);
				b.putString("GiftCount", me.GiftCount);
				b.putString("Introduction", me.Introduction);
				b.putString("LinkmeCount", me.LinkmeCount);
				b.putString("MemberName", me.MemberName);
				b.putString("MylinkCount", me.MylinkCount);
				b.putString("PhotoUrl", me.PhotoUrl);
				b.putString("Blood", me.Blood);
				b.putString("Age", me.Age);
				msg.setData(b);
				break;
			}
			case Task.TASK_MYDYNAMIC_LIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				// obj.put("Area", params.get("Area"));
				// obj.put("Classify", params.get("Classify"));
				// obj.put("JingDu", params.get("JingDu"));
				// obj.put("WeiDu", params.get("WeiDu"));
				obj.put("Pager", pager);
				String url = SERVECE_IP + "a=UserLoadNewsList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<MyDynamicEntry> l = new ArrayList<MyDynamicEntry>();
				msg.obj = JsonUtil.getMyDynamicList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_NEARBY_LIST: {
				// http://124.237.121.75:8000/webservice.aspx?m=uim&a=UserLoadNearUserList&
				// p={"JingDu":"23.3434","WeiDu":"45.5645","Pager":{"PageIndex":"1","ReturnCount":"20"},"AccountId":"hlf","Sex":"-1","LogInDate":"900"}
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));

				JSONObject obj2 = new JSONObject();
				obj2.put("PageIndex", "1");
				obj2.put("ReturnCount", "10");

				JSONArray objInterfaceCcotent = new JSONArray();
				// JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("JingDu", "100.0000");
				obj.put("WeiDu", "80.0000");
				obj.put("Sex", "-1");
				obj.put("LogInDate", "900");
				obj.put("Pager", obj2);

				// objInterfaceCcotent.put(obj);
				// objInterfaceCcotent.put(obj);

				String url = SERVECE_IP + "m=uim&a=UserLoadNearUserList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<NearbyFriendsEntry> l = new ArrayList<NearbyFriendsEntry>();
				msg.obj = JsonUtil.getMyNearbyList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_NEARBY_SHIOLIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Area", params.get("Area"));
				obj.put("Classify", params.get("Classify"));
				obj.put("JingDu", params.get("JingDu"));
				obj.put("WeiDu", params.get("WeiDu"));
				obj.put("Pager", pager);
				String url = SERVECE_IP + "a=UserLoadNearShopList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<NearByStoreEntry> l = new ArrayList<NearByStoreEntry>();
				msg.obj = JsonUtil.getNearByStoreList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_NEARBY_SHOPDETAIL: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("AccountFrom", params.get("AccountFrom"));
				String url = SERVECE_IP + "a=UserLoadShopDetail&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<MyDynamicEntry> l = new ArrayList<MyDynamicEntry>();
				msg.obj = JsonUtil.getUserLoadShopDetail(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_ACCOUNT_DETAIL: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				String url = SERVECE_IP + "a=UserLoadProfessionalInfo&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				AccountDetailEntry l = new AccountDetailEntry();
				msg.obj = JsonUtil.getUserLoadProfessionalInfo(
						myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_USERPHOTO_DETAIL: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("Id", params.get("Id"));
				String url = SERVECE_IP + "a=UserLoadPhoto&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				MyPhotoDetailEntry m = new MyPhotoDetailEntry();
				List<UserLoadCommentListEntry> l = new ArrayList<UserLoadCommentListEntry>();
				msg.obj = JsonUtil.getPhotoCommentList(myHttpCon.doGet(url), l,
						m);
				Bundle b = new Bundle();
				b.putString("AccountId", m.AccountId);
				b.putString("PhotoUrl", m.PhotoUrl);
				b.putString("MemberName", m.MemberName);
				b.putString("FilePath", m.FilePath);
				b.putString("FileDesc", m.FileDesc);
				b.putString("AddressInfo", m.AddressInfo);
				b.putString("DeviceInfo", m.DeviceInfo);
				b.putString("ReadCount", m.ReadCount);
				b.putString("PraiseCount", m.PraiseCount);
				b.putString("CommentCount", m.CommentCount);
				msg.setData(b);
				break;
			}
			case Task.TASK_USER_UPDATEINTRODUCTION: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Introduction", params.get("Introduction"));
				String url = SERVECE_IP + "a=UserUpdateIntroduction&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				StoreDetailEntry m = new StoreDetailEntry();
				msg.obj = JsonUtil.getUserUpdateIntroduction(
						myHttpCon.doGet(url), m);
				break;
			}
			case Task.TASK_USER_EDITDATA: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Height", params.get("Height"));
				obj.put("Hobby", params.get("Hobby"));
				obj.put("Homepage", params.get("Homepage"));
				obj.put("IsMarry", params.get("IsMarry"));
				obj.put("PalFor", params.get("PalFor"));
				obj.put("Profession", params.get("Profession"));
				obj.put("Salary", params.get("Salary"));
				obj.put("School", params.get("School"));

				String url = SERVECE_IP + "a=UserUpdateProfessionalInfo&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				AccountDetailEntry m = new AccountDetailEntry();
				msg.obj = JsonUtil.getUserUpdateProfessionalInfo(
						myHttpCon.doGet(url), m);
				break;
			}
			case Task.TASK_OTHER_HOMEPAGE: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONArray objInterfaceName = new JSONArray();
				objInterfaceName.put("UserLoadPersonalPage");
				objInterfaceName.put("UserLoadGiftList");

				JSONArray objInterfaceCcotent = new JSONArray();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountFrom"));
				obj.put("AccountFrom", lscManager.getInstance().myAccount.name);
				obj.put("Pager", pager);
				objInterfaceCcotent.put(obj.toString());
				objInterfaceCcotent.put(obj.toString());

				String url = SERVECE_IP
						+ "Composite=1&a="
						+ myHttpCon.getEncordUrl(objInterfaceName.toString())
						+ "&p="
						+ myHttpCon
								.getEncordUrl(objInterfaceCcotent.toString())
						+ ver;
				Bundle b = new Bundle();
				MyDataEntry me = new MyDataEntry();
				List<UserLoadGiftListEntry> l = new ArrayList<UserLoadGiftListEntry>();
				msg.obj = JsonUtil.getUserLoadPersonalPage(
						myHttpCon.doGet(url), l, me);
				b.putString("e", me.e);
				b.putString("GiftCount", me.GiftCount);
				b.putString("Introduction", me.Introduction);
				b.putString("LinkmeCount", me.LinkmeCount);
				b.putString("MemberName", me.MemberName);
				b.putString("MylinkCount", me.MylinkCount);
				b.putString("PhotoUrl", me.PhotoUrl);
				b.putString("Blood", me.Blood);
				b.putString("Age", me.Age);
				msg.setData(b);
				break;
			}
			case Task.TASK_USER_PHOTOLIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));

				String url = SERVECE_IP + "a=UserLoadAlbum&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<PhotoListEntry> l = new ArrayList<PhotoListEntry>();
				msg.obj = JsonUtil
						.getUserLoadAlbumList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_OTHER_PHOTOLIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));

				String url = SERVECE_IP + "a=UserLoadAlbum&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<PhotoListEntry> l = new ArrayList<PhotoListEntry>();
				msg.obj = JsonUtil
						.getUserLoadAlbumList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_USER_FRIENDSLIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("FriendType", params.get("FriendType"));
				obj.put("Pager", pager);

				String url = SERVECE_IP + "a=UserLoadFriendList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<MyFriendListEntry> l = new ArrayList<MyFriendListEntry>();
				msg.obj = JsonUtil.getUserLoadFriendList(myHttpCon.doGet(url),
						l);
				break;
			}
			case Task.TASK_OTHERMYDYNAMIC_LIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Pager", pager);
				String url = SERVECE_IP + "a=UserLoadNewsList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<MyDynamicEntry> l = new ArrayList<MyDynamicEntry>();
				msg.obj = JsonUtil.getMyDynamicList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_USER_SAWHELLO: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				String url = SERVECE_IP + "a=UserSayHeyToUser&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				AccountDetailEntry l = new AccountDetailEntry();
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_JOINFRIENDS: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				obj.put("Relation", params.get("Relation"));
				String url = SERVECE_IP + "a=UserMakeRelation&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_LINKME: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				obj.put("Relation", params.get("Relation"));
				String url = SERVECE_IP + "a=UserMakeRelation&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_INBLACK: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				obj.put("Relation", params.get("Relation"));
				String url = SERVECE_IP + "a=UserMakeRelation&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_USER_REPORT: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountFrom", params.get("AccountFrom"));
				obj.put("AccountTo", params.get("AccountTo"));
				String url = SERVECE_IP + "a=UserReportUser&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			case Task.TASK_STOREMYDYNAMIC_LIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Pager", pager);
				String url = SERVECE_IP + "a=UserLoadNewsList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<MyDynamicEntry> l = new ArrayList<MyDynamicEntry>();
				msg.obj = JsonUtil.getMyDynamicList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_STORE_PHOTOLIST: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));

				String url = SERVECE_IP + "a=UserLoadAlbum&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<PhotoListEntry> l = new ArrayList<PhotoListEntry>();
				msg.obj = JsonUtil
						.getUserLoadAlbumList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_USER_NOTIFICATION: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("Pager", pager);
				String url = SERVECE_IP + "a=UerLoadNoticeList&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				List<NotificationCenterEntry> l = new ArrayList<NotificationCenterEntry>();
				msg.obj = JsonUtil
						.getUerLoadNoticeList(myHttpCon.doGet(url), l);
				break;
			}
			case Task.TASK_PHOTO_ADDPRAISE: {
				Map<String, String> params = lasttask.getTaskParams();
				JSONObject obj = new JSONObject();
				obj.put("AccountId", params.get("AccountId"));
				obj.put("ObjectKind", params.get("ObjectKind"));
				obj.put("ObjectNo", params.get("ObjectNo"));
				String url = SERVECE_IP + "a=UserAddPraise&p="
						+ myHttpCon.getEncordUrl(obj.toString()) + ver;
				msg.obj = JsonUtil.getLoginData(myHttpCon.doGet(url));
				break;
			}
			}

			msg.what = lasttask.getTaskID();
		} catch (Exception e) {
			Log.d("server",
					"task id=" + lasttask.getTaskID() + "" + e.toString());
			msg.what = lasttask.getTaskID();
		}

		allTask.remove(lasttask);// 完成任务
		hand.sendMessage(msg);
	}

	/**
	 * 更新UI。
	 */
	public Handler hand = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			BaseActivity activity = null;
			switch (msg.what) {
			case Task.TASK_USER_LOGIN:
				activity = MainService.getActivityByName("Register");
				if (activity != null)
					activity.refresh(Task.TASK_USER_LOGIN, msg.obj);
				break;
			case Task.TASK_USER_REGISTER:
				activity = MainService.getActivityByName("Register");
				if (activity != null)
					activity.refresh(Task.TASK_USER_REGISTER, msg.obj);
				break;
			case Task.TASK_RESET_PASSWORD:
				activity = MainService.getActivityByName("Register");
				if (activity != null)
					activity.refresh(Task.TASK_USER_REGISTER, msg.obj);
				break;
			case Task.TASK_MYDYNAMIC_LIST:

				activity = MainService.getActivityByName("LscActivity");
				if (activity != null)
					activity.refresh(Task.TASK_MYDYNAMIC_LIST, msg.obj);
				break;
			case Task.TASK_NEARBY_LIST:

				activity = MainService.getActivityByName("LscActivity");
				if (activity != null)
					activity.refresh(Task.TASK_NEARBY_LIST, msg.obj);
				break;
			case Task.TASK_NEARBY_SHIOLIST:
				activity = MainService.getActivityByName("LscActivity");
				if (activity != null)
					activity.refresh(Task.TASK_NEARBY_SHIOLIST, msg.obj);
				break;
			case Task.TASK_NEARBY_SHOPDETAIL:
				activity = MainService.getActivityByName("NearByStoreDetail");
				if (activity != null)
					activity.refresh(Task.TASK_NEARBY_SHOPDETAIL, msg.obj);
				break;
			case Task.TASK_USER_HOME: {
				MyDataEntry me = new MyDataEntry();
				Bundle b = msg.getData();
				me.e = b.getString(me.e);
				me.GiftCount = b.getString("GiftCount");
				me.Introduction = b.getString("Introduction");
				me.LinkmeCount = b.getString("LinkmeCount");
				me.MemberName = b.getString("MemberName");
				me.MylinkCount = b.getString("MylinkCount");
				me.PhotoUrl = b.getString("PhotoUrl");
				me.Blood = b.getString("Blood");
				me.Age = b.getString("Age");
				activity = MainService.getActivityByName("LscActivity");
				activity.refresh(Task.TASK_USER_HOME, msg.obj, me);
				break;
			}
			case Task.TASK_ACCOUNT_DETAIL:
				activity = MainService.getActivityByName("EditMyData");
				if (activity != null)
					activity.refresh(Task.TASK_ACCOUNT_DETAIL, msg.obj);
				break;
			case Task.TASK_USER_EDITDATA:
				activity = MainService.getActivityByName("EditMyData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_EDITDATA, msg.obj);
				break;
			case Task.TASK_OTHER_HOMEPAGE: {
				MyDataEntry me = new MyDataEntry();
				Bundle b = msg.getData();
				me.e = b.getString(me.e);
				me.GiftCount = b.getString("GiftCount");
				me.Introduction = b.getString("Introduction");
				me.LinkmeCount = b.getString("LinkmeCount");
				me.MemberName = b.getString("MemberName");
				me.MylinkCount = b.getString("MylinkCount");
				me.PhotoUrl = b.getString("PhotoUrl");
				me.Blood = b.getString("Blood");
				me.Age = b.getString("Age");
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_OTHER_HOMEPAGE, msg.obj, me);
				break;
			}
			case Task.TASK_USER_PHOTOLIST: {
				activity = MainService.getActivityByName("LscActivity");
				if (activity != null)
					activity.refresh(Task.TASK_USER_PHOTOLIST, msg.obj);
				break;
			}
			case Task.TASK_OTHER_PHOTOLIST: {
				activity = MainService.getActivityByName("OtherFriendData");
				activity.refresh(Task.TASK_OTHER_PHOTOLIST, msg.obj);
				break;
			}
			case Task.TASK_USER_FRIENDSLIST:
				activity = MainService.getActivityByName("LscActivity");
				if (activity != null)
					activity.refresh(Task.TASK_USER_FRIENDSLIST, msg.obj);
				break;
			case Task.TASK_OTHERMYDYNAMIC_LIST:
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_OTHERMYDYNAMIC_LIST, msg.obj);
				break;
			case Task.TASK_USERPHOTO_DETAIL:
				MyPhotoDetailEntry me = new MyPhotoDetailEntry();
				Bundle b = msg.getData();
				me.AccountId = b.getString("AccountId");
				me.PhotoUrl = b.getString("PhotoUrl");
				me.MemberName = b.getString("MemberName");
				me.FilePath = b.getString("FilePath");
				me.FileDesc = b.getString("FileDesc");
				me.AddressInfo = b.getString("AddressInfo");
				me.DeviceInfo = b.getString("DeviceInfo");
				me.ReadCount = b.getString("ReadCount");
				me.PraiseCount = b.getString("PraiseCount");
				me.CommentCount = b.getString("CommentCount");
				activity = MainService.getActivityByName("MyPhotoDetail");
				if (activity != null)
					activity.refresh(Task.TASK_USERPHOTO_DETAIL, msg.obj, me);
				break;
			case Task.TASK_USER_SAWHELLO: {
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_SAWHELLO, msg.obj);
				break;
			}
			case Task.TASK_USER_JOINFRIENDS: {
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_JOINFRIENDS, msg.obj);
				break;
			}
			case Task.TASK_USER_LINKME: {
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_LINKME, msg.obj);
				break;
			}
			case Task.TASK_USER_INBLACK: {
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_INBLACK, msg.obj);
				break;
			}
			case Task.TASK_USER_REPORT: {
				activity = MainService.getActivityByName("OtherFriendData");
				if (activity != null)
					activity.refresh(Task.TASK_USER_REPORT, msg.obj);
				break;
			}
			case Task.TASK_STOREMYDYNAMIC_LIST: {
				activity = MainService.getActivityByName("NearByStoreDetail");
				if (activity != null)
					activity.refresh(Task.TASK_STOREMYDYNAMIC_LIST, msg.obj);
				break;
			}
			case Task.TASK_USER_NOTIFICATION: {
				activity = MainService.getActivityByName("NotificationCenter");
				if (activity != null)
					activity.refresh(Task.TASK_USER_NOTIFICATION, msg.obj);
				break;
			}
			case Task.TASK_STORE_PHOTOLIST: {
				activity = MainService.getActivityByName("NearByStoreDetail");
				if (activity != null)
					activity.refresh(Task.TASK_STOREMYDYNAMIC_LIST, msg.obj);
				break;
			}
			case Task.TASK_PHOTO_ADDPRAISE: {
				activity = MainService.getActivityByName("MyPhotoDetail");
				if (activity != null)
					activity.refresh(Task.TASK_PHOTO_ADDPRAISE, msg.obj);
				break;
			}
			}
		}

	};

	public static void AddNewTask(Task task) {
		allTask.add(task);
	}

	public static void AddActivity(BaseActivity activity) {
		allActivity.add(activity);
	}

	public static BaseActivity getActivityByName(String name) {
		BaseActivity ia = null;
		for (BaseActivity ac : allActivity) {
			if (ac.getClass().getName().indexOf(name) >= 0) {
				ia = ac;
			}
		}
		return ia;
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	// 提示是否退出应用程序
	public static void promptExitApp(final Activity context) {
		// 创建对话框
		// AlertDialog.Builder ab = new AlertDialog.Builder(context);
		// ab.setTitle("提示");
		// ab.setMessage("是否退出？");
		// ab.setPositiveButton(R.string.exit_ok, new OnClickListener() {
		// @Override
		// public void onClick(DialogInterface arg0, int arg1) {
		// arg0.dismiss();
		// exitApp(context);// 退出整个应用
		// }
		// });
		// ab.setNegativeButton(R.string.exit_cancle, null);
		// ab.create().show();
	}

	// 退出应用程序
	public static void exitApp(Activity context) {// 退出所有Activity
		for (int i = 0; i < allActivity.size(); i++) {
			((Activity) allActivity.get(i)).finish();
		}
		allActivity.clear();

		MainService.isRun = false;

		if (LscActivity.self != null)
			LscActivity.self.finish();
		context.stopService(new Intent("com.lsc.logic.mainlogic"));
		if (mainService != null)
			mainService.stopSelf();
		Log.d("server", "exit...");
	}

}
